package com.atguigu.daijia.common.login;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author zqtao
 */
@Slf4j
@Component
@Aspect
@Order(100)
public class JiYunLoginAspect {

    @Autowired
    private RedisTemplate redisTemplate;


    @Around("execution(* com.atguigu.daijia.*.controller.*.*(..)) && @annotation(jiYunLogin)")
    public Object process(ProceedingJoinPoint joinPoint, JiYunLogin jiYunLogin) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = null;
        if (sra != null) {
            request = sra.getRequest();
        }
        if (request != null) {
            String token = request.getHeader("token");
            if(!StringUtils.hasText(token)) {
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }
            String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX+token);
            if(StringUtils.hasText(customerId)) {
                AuthContextHolder.setUserId(Long.parseLong(customerId));
            }
            return joinPoint.proceed();
        }
        AuthContextHolder.setUserId(null);
        return joinPoint.proceed();
    }


}
